<template>
    <div>
        <el-upload class="upload-demo" :action='urlUpdate' :on-change="handleChange" :on-remove="handleRemove"
            :on-success="handleSuccess" :on-error="handleError" :file-list="fileList" :on-preview="handlePreview" :headers="token">
            <el-button size="mini" type="primary">点击上传</el-button>
        </el-upload>
    </div>
</template>
  
<script>
import { treeClassTranslate } from "../../../utils/treeDataTranslate.js";
export default {
    emits: ["refreshDataList"],
    props:{
        fileLists:Array,
        num:String
    },
    data() {
        return {
            urlUpdate: '',//文件上传路径
            token: {
                token: ''
            },
            formLabelWidth: "120px",
            fileList: [],//规格书
            // url: process.env.VUE_APP_BASE_API,
        };
    },
    created() {
        this.urlUpdate = process.env.VUE_APP_BASE_API + '/file/uploadFileInterface'
        this.token.token = sessionStorage.getItem("Token");
        console.log(this.token)
        console.log(this.fileLists)
    },
    watch:{
        fileLists:{
				handler(newVal) {
					this.fileList=newVal
				},
				deep: true
			}
    },
    methods: {
        async init(id, val) {

        },
        //上传文件
        handleChange(file, fileList) {
            console.log(fileList)
            if(this.num=='1'){
                this.fileList = fileList;
            }else{
                console.log(fileList)
                if (fileList.length > 1) {
			        fileList=[fileList[fileList.length - 1]];
					this.fileList = [fileList[fileList.length - 1]];
                    console.log(this.fileList)
			    }else{
                    this.fileList=fileList
                }
            }
            // 
           
            
        },
        // 点击文件下载
        handlePreview(file) { this.fileDown(file); },
        handleRemove(file, fileList){
            console.log(fileList)
            this.fileList = fileList;
            this.$emit('uploadFile',this.fileList)
        },
        //附件上传成功回调
        handleSuccess(response, file, fileList) {
            console.log(response, file, fileList)
            if(response.code==200){
                this.$emit('uploadFile',this.fileList)
            }else{
                for(let i=0;i<fileList.length;i++){
                    if(fileList[i].uid==file.uid){
                        fileList.splice(i, 1);
                    }
                }
                this.$message({ message: response.msg, type: 'error', duration: 1000 })
            }
         },
         handleError(err, file, fileList){
            console.log(err)
            // this.$message({ message: response.msg, type: 'error', duration: 1000 })
         }
    },
};
</script>
  
<style scoped>
</style>
  